package com.example.xjt.safe;

import com.example.xjt.orm.role.RoleDO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 实现security提供的UserDetails
 */
public class SafePrincipal implements UserDetails {

    @Override
    public String toString() {
        return "SafePrincipal{" +
                "enabled=" + enabled +
                ", roleList=" + roleList +
                ", uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    //安全用户的 是否启用---security-UserDetails-规定的属性
    private Boolean enabled;

    //ORM层角色列表
    private List<RoleDO> roleList;


    //对应-ORM层-用户id
    private Long uid;


    //对应-ORM层-用户账号
    private String username;

    //对应-ORM层-用户密码
    private String password;


    //----------------------------------------------------
    public Long getUid() {
        return uid;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    //--------------SafePrincipal 授权XXX角色---------------------------------------
    @Override
    @JsonIgnore //不序列化 UserDetails内部的属性
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>(roleList.size());
        for (RoleDO role : roleList) {
            authorities.add(new SimpleGrantedAuthority(role.getRoleFlag()));
        }
        return authorities;
    }


    //-----------安全用户的角色列表-------------------------------------
    public void setRoleList(List<RoleDO> roleList) {
        this.roleList = roleList;
    }

    public List<RoleDO> getRoleList() {
        return roleList;
    }


    //-------------安全用户的 用户名------------------------------------------------
    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    //-------------安全用户的 密码------------------------------------------------

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getPassword() {
        return password;
    }

    //-------------安全用户的 是否启用了------------------------------------------------

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    //-----------------安全用户的 账号是否过期/被锁，凭证是否过期----------------------

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }


}
